﻿using CodeBuilder.DTO;
using CodeBuilder.Entity;
using CodeBuilder.Repository;
using CodeBuilder.Service;
using CodeBuilder.ViewModel;
using CodeBuilder.Reader;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using System.Reflection;

namespace CodeBuilder
{
    public class MyBuilder
    {
        public void Run()
        {

            IConfiguration config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .Build();

            string connectionString = config["ConnectionStrings:DBConnetion"] ?? string.Empty;
            string dtoPath = config["FilePaths:DtoPath"] ?? AppContext.BaseDirectory;
            string entityPath = config["FilePaths:EntityPath"] ?? AppContext.BaseDirectory;
            string repositoryPath = config["FilePaths:RepositoryPath"] ?? AppContext.BaseDirectory;
            string servicePath = config["FilePaths:ServicePath"] ?? AppContext.BaseDirectory;
            string viewModelPath = config["FilePaths:ViewModelPath"] ?? AppContext.BaseDirectory;

            while (true)
            {
                Console.WriteLine("请输入表名称,然后按《Enter》键");
                Console.WriteLine("多个表格请用\",\"隔开");

                var inputs = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(inputs))
                    continue;
                var inputList = inputs.Split(',');
                foreach (var input in inputList)
                {
                    var filePath = Path.Combine(AppContext.BaseDirectory, input);
                    #region read table
                    MyReader readTable = new MyReader(connectionString);
                    DataTable schemaTable = readTable.ReadTable($"SHOW COLUMNS FROM {input};");
                    #endregion
                    #region read index
                    MyReader readIndex = new MyReader(connectionString);
                    DataTable indexTable = readIndex.ReadTable($"SHOW INDEX FROM {input};");
                    #endregion

                    CreateEntityBase createEntityBase = new CreateEntityBase(input, filePath, schemaTable, indexTable);
                    createEntityBase.Run();

                    CreateEntity createEntity = new CreateEntity(input, entityPath, schemaTable, indexTable);
                    createEntity.Run();

                    CreateIBaseRepository createIBaseRepository = new CreateIBaseRepository(input, filePath);
                    createIBaseRepository.Run();

                    CreateIRepository createIRepository = new CreateIRepository(input, repositoryPath, schemaTable, indexTable);
                    createIRepository.Run();

                    CreateRepository createRepository = new CreateRepository(input, repositoryPath, schemaTable, indexTable);
                    createRepository.Run();

                    CreateBaseRepository createBaseRepository = new CreateBaseRepository(input, filePath);
                    createBaseRepository.Run();

                    CreateDtoBase createDtoBase = new CreateDtoBase(input, filePath, schemaTable);
                    createDtoBase.Run();

                    CreateDto createDto = new CreateDto(input, dtoPath, schemaTable);
                    createDto.Run();

                    CreateIBaseService createIBaseService = new CreateIBaseService(input, filePath);
                    createIBaseService.Run();

                    CreateIService createIService = new CreateIService(input, servicePath, schemaTable, indexTable);
                    createIService.Run();

                    CreateBaseService createBaseService = new CreateBaseService(input, filePath);
                    createBaseService.Run();

                    CreateService createService = new CreateService(input, servicePath, schemaTable, indexTable);
                    createService.Run();

                    CreateBaseViewModel createBaseViewModel = new CreateBaseViewModel(input, filePath);
                    createBaseViewModel.Run();

                    CreateViewModel createViewModel = new CreateViewModel(input, viewModelPath);
                    createViewModel.Run();

                    //TODO:
                    //Console.WriteLine("请选择生成Page、Content、Popup");
                    //Console.ReadLine();
                    //conn.Close();
                }
                Console.WriteLine("创建完成！");
                Console.WriteLine("是否继续：(y or n)");

                var result = Console.ReadKey();
                if (result.KeyChar.ToString().ToLower() == "y")
                {
                    continue;
                }
                else
                {
                    break;
                }
            }
        }
    }
}
